﻿using AutoMapper;
using Enter.Admin.Entities;
using Enter.Admin.Filters;
using Enter.Admin.Models;
using Enter.Admin.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;

namespace Enter.Admin.Controllers
{
    [TypeFilter(typeof(ActionFilter), Arguments = new object[] { ActionRole.Admin })]
    public class SettingController : BaseController
    {
        private readonly IConfiguration _configuration;
        private readonly Upload _upload;
        private readonly Mail _mail;

        public SettingController(ILogger<SettingController> logger, IFreeSql fsql, IMapper mapper, IConfiguration configuration, Upload upload, Mail mail) : base(logger, fsql, mapper)
        {
            _configuration = configuration;
            _upload = upload;
            _mail = mail;
        }

        // GET: SettingController
        public ActionResult Index()
        {
            var model = _fsql.Select<Setting>().ToList();
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(List<Setting> data)
        {
            _fsql.Transaction(() => {
                foreach (var item in data)
                {
                    if (string.IsNullOrWhiteSpace(item.Value)) 
                    { 
                        item.Value = string.Empty;
                    }
                    item.Value = item.Value.Trim();
                    _fsql.Update<Setting>().Set(t => t.Value, item.Value).Where(t => t.Id == item.Id).ExecuteAffrows();
                }
            });
            var settings = _fsql.Select<Setting>().ToList();
            foreach (var setting in settings)
            {
                _configuration[setting.Key] = setting.Value;
            }
            return Json(new ResponseResult<string>(true, "更新成功."));
        }

        // GET: SettingController/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: SettingController/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: SettingController/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: SettingController/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: SettingController/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: SettingController/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: SettingController/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}
